
package pe.pucp.netbeans.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
import org.openide.util.Exceptions;

/**
 *
 * @author Diego
 */
public abstract class ReaderThread extends Thread {
    private InputStream is;
    ReaderThread(InputStream is) {
        this.is = is;
    }
    protected abstract String nuevaEntrada(String entrada);

    @Override
    public void run() {
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line = null;
            while ( (line = br.readLine()) != null) {
                nuevaEntrada(line);
            }
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(null, "Error:"+ioe.toString());
            ioe.printStackTrace();
        }
        JOptionPane.showMessageDialog(null, "se va cerrar reader thread");
        try {
            br.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        try {
            isr.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        try {
            is.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}
